/**
 *FontChooserPane.java
 *@anthor Daniel Liu
 *2006-5-9 ����02:27:16
 **/
package org.lex.swing.fontchooser.advanced;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.ChangeListener;

import org.lex.swing.fontchooser.FontStyle;
import org.lex.swing.fontchooser.simple.SimpleFontAttribute;
import org.lex.swing.fontchooser.simple.SimpleFontChooserPane;
import org.lex.swing.fontchooser.simple.SimpleFontFactory;


public class AdvancedFontChooserPane extends JPanel {
	private AdvancedPane advancedPane = null;
	private SimpleFontChooserPane simpleFontChooserPane = null;
	private JScrollPane previewScrollPane = null;
	private JSplitPane splitPane = null;
	private JTabbedPane tabbedPane = null;
	private JTextArea textArea = null;

	public AdvancedFontChooserPane() {
		super();
		initialize();
	}

	public void addFontChangeListener(ChangeListener listener) {

	}

	public void removeFontChangeListener(ChangeListener listener) {

	}

	/**
	 * ����GUI
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 320);
		this.add(getSplitPane(), BorderLayout.CENTER);
	}

	private AdvancedPane getAdvancedPane() {
		if (advancedPane == null) {
			advancedPane = new AdvancedPane();
			// advancedPane.addPropertyChangeListener( this );
		}
		return advancedPane;
	}

	private SimpleFontChooserPane getGeneralPane() {
		if (simpleFontChooserPane == null) {
			simpleFontChooserPane = new SimpleFontChooserPane();
			simpleFontChooserPane.addFontChangeListener(new SimpleFontAttributeChangeHandler());
		}
		return simpleFontChooserPane;
	}

	private JScrollPane getPreviewScrollPane() {
		if (previewScrollPane == null) {
			previewScrollPane = new JScrollPane();
			previewScrollPane.setViewportView(getTextArea());
		}
		return previewScrollPane;
	}

	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
			splitPane.setDividerSize(5);
			splitPane.setResizeWeight(0.7D);
			splitPane.setBottomComponent(getPreviewScrollPane());
			splitPane.setTopComponent(getTabbedPane());
		}
		return splitPane;
	}

	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane();
			tabbedPane.addTab("Basic", null, getGeneralPane(), null);
			tabbedPane.addTab("Advanced", null, getAdvancedPane(), null);
		}
		return tabbedPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea(3, 20);
			textArea.setText("The quick brown fox jumped over the lazy dogs");
			textArea.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
		}
		return textArea;
	}

	class SimpleFontAttributeChangeHandler implements PropertyChangeListener {
		private SimpleFontFactory factory = new SimpleFontFactory();

		public void propertyChange(PropertyChangeEvent e) {
			String property = e.getPropertyName();
			Object newValue = e.getNewValue();
			System.out.println("Attribute: " + property + "\tValue= " + newValue);

			SimpleFontAttribute sfa = SimpleFontAttribute.valueOf(property);
			if (sfa == SimpleFontAttribute.STYLE)
				newValue = FontStyle.parseFromDiaplayName(newValue.toString()).getStyle();
			factory.set(SimpleFontAttribute.valueOf(property), newValue);
			getTextArea().setFont(factory.newFont());
		}
	}

	class AdvancedFontAttributeChangeHandler implements PropertyChangeListener {
		@SuppressWarnings("unchecked")
		Map<TextAttribute, Object> attributes = (Map<TextAttribute, Object>) new Font("Dialog", Font.PLAIN, 12).getAttributes();

		public void propertyChange(PropertyChangeEvent e) {
			String property = e.getPropertyName();
			Object newValue = e.getNewValue();
			System.out.println("Attribute: " + property + "\tValue= " + newValue);
			attributes.put(AdvancedFontAttribute.valueOf(property).getAttribute(), newValue);
			getTextArea().setFont(new Font(attributes));
		}
	}
}
